var myConfig = {
        height: '150px',
        width: '380px',
        animate: true,
        dompath: false,
        focusAtStart: true,
		       toolbar: {
            titlebar: '',
      
            buttons: [
                { group: 'insertitem', label: '',
                    buttons: [
						{ type: 'push', label: 'HTML Link CTRL + SHIFT + L', value: 'createlink', disabled: true }
                    ]
                }
            ]
        }

    };
    myEditor = new YAHOO.widget.Editor('editor', myConfig);
    YAHOO.util.Event.onAvailable('iconMenu', function() {
        YAHOO.util.Event.on('iconMenu', 'click', function(ev) {
            var tar = YAHOO.util.Event.getTarget(ev);
            if (tar.tagName.toLowerCase() == 'img') {
                var img = tar.getAttribute('src', 2);
                var _button = this.toolbar.getButtonByValue('inserticon');
                _button._menu.hide();
                this.toolbar.fireEvent('inserticonClick', { type: 'inserticonClick', icon: img });
            }
            YAHOO.util.Event.stopEvent(ev);
        }, myEditor, true);
    });


    myEditor.on('toolbarLoaded', function() { 
       
        var imgConfig = {
            type: 'push', label: 'Insert Icon', value: 'inserticon',
            menu: function() {
                var menu = new YAHOO.widget.Overlay('inserticon', { width: '220px', height: '220px', visible: false });
                var str = '';
				var j = 1;
                for (var a = 0; a < 4; a++) {
                    for (var i = 0; i < 4; i++) {	
                        str += '<a href="#"><img src="img/face/' + j + '.gif" border="0"></a>';
						j = j+1;
                    }
                }
                menu.setBody('<div id="iconMenu">' + str + '</div>');
                menu.beforeShowEvent.subscribe(function() {
                    menu.cfg.setProperty('context', [myEditor.toolbar.getButtonByValue('inserticon').get('element'), 'tl', 'bl']);
                });            
                menu.render(document.body);
                menu.element.style.visibility = 'hidden';
                return menu;
            }()
        };
        myEditor.toolbar.addButtonToGroup(imgConfig, 'insertitem');

        myEditor.toolbar.on('inserticonClick', function(ev) {
            var icon = '';
            this._focusWindow();
            if (ev.icon) {
                icon = ev.icon;
            }
            this.execCommand('inserthtml', '<img src="' + icon + '" border="0">');
            return false;
        }, myEditor, true);

    });
    myEditor.render();
 //Add String object a trim() function
    String.prototype.trim = function()
	{
    	return this.replace(/(^\s*)|(\s*$)/g, "");
	} 
	
function postComment(userId,knowledgeId){
	myEditor.saveHTML();
	var content = myEditor.get('textarea').value;
	if(content.trim() == ""){
		simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1", { width: "300px",
																			   fixedcenter: true,
																			   visible: true,
																			   draggable: false,
																			   close: true,
																			   text: "\u8bc4\u8bba\u5185\u5bb9\u4e0d\u80fd\u4e3a\u7a7a!",
																			   constraintoviewport: true,
																			   buttons: [{ text:"\u786e\u5b9a", handler:function(){this.hide();}, isDefault:true }]
																						   
																			 } );
	simpledialog1.setHeader("\u63d0\u793a");
	simpledialog1.render("container");
		return;
		}
	var handleSuccess = function(o){
	//alert(o.responseText);
	myEditor.setEditorHTML("");
	var html = o.responseText;
	var new_comment = document.createElement('div');
	new_comment.innerHTML = html;
	document.getElementById('commentlist').appendChild(new_comment);
}
var handleFailure = function(o){
	alert("Failure");
}
	var replierId = document.getElementById('replierId').value;
	var postData = "userId=" + userId + "&knowledgeId=" + knowledgeId + "&replierId=" + replierId + "&content=" + content;
	var callback ={success:handleSuccess,failure:handleFailure};
	YAHOO.util.Connect.asyncRequest('POST', "addcomment.action", callback, postData);
}
function delComment(id){
	var handleYes = function() {
		var postData = "id=" + id;
		YAHOO.util.Connect.asyncRequest('POST', "deletecomment.action",null, postData);
		var el = document.getElementById(id);
		try{
		document.getElementById('commentlist').removeChild(el);
		}catch(ex){
		 el = el.parentNode;
		 document.getElementById('commentlist').removeChild(el);
		}
		this.hide();
	};
	var handleNo = function() {
		this.hide();
	};
	simpledialog1 = new YAHOO.widget.SimpleDialog("simpledialog1", { width: "300px",
																			   fixedcenter: true,
																			   visible: true,
																			   draggable: false,
																			   close: true,
																			   text: "\u786e\u5b9a\u5220\u9664\u8bc4\u8bba\uff1f",
																			   constraintoviewport: true,
																			   buttons: [ { text:"\u786e\u5b9a", handler:handleYes, isDefault:true },
																						  { text:"\u53d6\u6d88",  handler:handleNo } ]
																			 } );
	simpledialog1.setHeader("\u5220\u9664\u8bc4\u8bba");
	simpledialog1.render("container");
}
function reply(replierName,replierId){
	var el = document.getElementById('replierId');
	el.value = replierId;
	document.getElementById('commentForm').appendChild(el);
	myEditor.setEditorHTML("\u56de\u590d" + replierName + ":");
}